package com.experienceBaby.Cashbook.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Message;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.experienceBaby.Cashbook.databinding.ActivityEditorBinding;
import com.experienceBaby.Cashbook.fragment.DetailFragment;
import com.experienceBaby.Cashbook.greendao.DbController;
import com.experienceBaby.Cashbook.greendao.db.Recode;
import com.xuexiang.xui.XUI;

public class EditorActivity extends AppCompatActivity {
    int id, year11;
    String xx, data;
    Recode recode;
    ActivityEditorBinding mBinding;

    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        XUI.init(getApplication());
        XUI.initTheme(this);
        super.onCreate(savedInstanceState);
        // View Binding
        mBinding = ActivityEditorBinding.inflate(getLayoutInflater());
        View rootView = mBinding.getRoot();
        setContentView(rootView);
        initData();
        initView();
    }

    private void initData() {
        id = getIntent().getIntExtra("id", 1);
        xx = getIntent().getStringExtra("xx");
        data = getIntent().getStringExtra("data");
        year11 = getIntent().getIntExtra("year11", 1);
        recode = new DbController(this).searchById(id);
    }

    private void initView() {
        mBinding.ivJiantou.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        double d = Double.valueOf(xx.split(" ")[2]);
        if (d > 0) {
            mBinding.tvq.setText("类型：  " + "收入");
        } else {
            mBinding.tvq.setText("类型：  " + "支出");
        }
        mBinding.tvw.setText("金额：  " + String.format("%.2f", Math.abs(d)));
        mBinding.tve.setText("日期：  " + year11 + "年" + data.split(" ")[0] + "  " + data.split(" ")[1]);
        mBinding.tvr.setText("类型：  " + xx.split(" ")[0]);

        mBinding.btb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mBinding.bts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                AlertDialog.Builder builder = new AlertDialog.Builder(EditorActivity.this);
//                builder.setTitle("确认删除");
//                builder.setMessage("删除后数据不可恢复！");
//                builder.setPositiveButton("是",null );
//                builder.setNegativeButton("否", null);
//                builder.show();
                Message msg = new Message();
                msg.what = 1;
                msg.arg1 = getIntent().getIntExtra("groupPosition", 1);
                msg.arg2 = getIntent().getIntExtra("childPosition", 1);
                DetailFragment.handler.sendMessage(msg);
                finish();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mBinding = null;
    }
}
